//
//  SettingView.cpp
//  firebkball
//
//  Created by ren jian on 18/5/31.
//
//

#include "SettingView.h"
#include "WelcomeLayer.h"

Scene *m_settingScene;
Scene *SettingView::createScene()
{
    m_settingScene = Scene::create();
    m_settingScene->addChild(SettingView::create());
    return m_settingScene;
}

bool SettingView::init()
{
    if (!Layer::init()) {
        return false;
    }
    m_selectedIndex = 0;
    m_size = Director::getInstance()->getWinSize();
    
    m_controller = CSLoader::createNode("res/setting.csb");
    m_controller->setAnchorPoint(Vec2(0.5, 0.5));
    m_controller->setPosition(m_size/2);
    this->addChild(m_controller);
    
    m_editNode = m_controller->getChildByName("l_box")->getChildren();
    //初始化按钮事件
    for (int i=0; i<m_editNode.size(); i++) {
        Node *l_node = m_editNode.at(i);
        string l_name = l_node->getName();
        if (l_name=="ly_close") {
            continue;
        }
        int l_number = DataStorageTool::getIntValue(l_name);
        if (l_number==0) {
            l_number = 2;
            DataStorageTool::saveIntValue(l_name, l_number);
        }
        auto l_lab_number = (cocos2d::ui::Text*)l_node->getChildByName("lab_number");
        l_lab_number->setString(StringUtils::format("%d", l_number));
    }
    
    cocos2d::ui::Button *l_btn_up = (cocos2d::ui::Button *)m_controller->getChildByName("btn_up");
    l_btn_up->addClickEventListener(CC_CALLBACK_1(SettingView::onClickUp, this));
    cocos2d::ui::Button *l_btn_down = (cocos2d::ui::Button *)m_controller->getChildByName("btn_down");
    l_btn_down->addClickEventListener(CC_CALLBACK_1(SettingView::onClickDown, this));
    cocos2d::ui::Button *l_btn_left = (cocos2d::ui::Button *)m_controller->getChildByName("btn_left");
    l_btn_left->addClickEventListener(CC_CALLBACK_1(SettingView::onClickLeft, this));
    cocos2d::ui::Button *l_btn_right = (cocos2d::ui::Button *)m_controller->getChildByName("btn_right");
    l_btn_right->addClickEventListener(CC_CALLBACK_1(SettingView::onClickRight, this));
    
    return true;
}

void SettingView::onEnter()
{
    Layer::onEnter();
    
}

void SettingView::onClickLeft(cocos2d::Ref *ref)
{
    Node *l_node = m_editNode.at(m_selectedIndex);
    string l_name = l_node->getName();
    if (l_name=="ly_close") {
        this->closeMe(nullptr);
        return;
    }
    
    auto l_lab_number = (cocos2d::ui::Text*)l_node->getChildByName("lab_number");
    int l_number = MyFunction::atoi(l_lab_number->getString().c_str());
    if (l_number<1) {
        return;
    }
    l_number--;
    l_lab_number->setString(StringUtils::format("%d", l_number));
    
    DataStorageTool::saveIntValue(l_name, l_number);
}

void SettingView::onClickRight(cocos2d::Ref *ref)
{
    Node *l_node = m_editNode.at(m_selectedIndex);
    string l_name = l_node->getName();
    if (l_name=="ly_close") {
        this->closeMe(nullptr);
        return;
    }
    
    auto l_lab_number = (cocos2d::ui::Text*)l_node->getChildByName("lab_number");
    int l_number = MyFunction::atoi(l_lab_number->getString().c_str());
    l_number++;
    l_lab_number->setString(StringUtils::format("%d", l_number));
    
    DataStorageTool::saveIntValue(l_name, l_number);
}

void SettingView::onClickUp(cocos2d::Ref *ref)
{
    if (m_selectedIndex<1) {
        return;
    }
    Node *l_lastNode = m_editNode.at(m_selectedIndex);
    l_lastNode->getChildByName("l_bg")->setVisible(false);
    m_selectedIndex--;
    CCLOG("目标: %d", m_selectedIndex);
    Node *l_nowNode = m_editNode.at(m_selectedIndex);
    l_nowNode->getChildByName("l_bg")->setVisible(true);
}

void SettingView::onClickDown(cocos2d::Ref *ref)
{
    if (m_selectedIndex>=m_editNode.size()) {
        return;
    }
    Node *l_lastNode = m_editNode.at(m_selectedIndex);
    l_lastNode->getChildByName("l_bg")->setVisible(false);
    m_selectedIndex++;
    CCLOG("down 目标: %d", m_selectedIndex);
    Node *l_nowNode = m_editNode.at(m_selectedIndex);
    l_nowNode->getChildByName("l_bg")->setVisible(true);
}

void SettingView::closeMe(cocos2d::Ref *ref)
{
    Director::getInstance()->replaceScene(WelcomeLayer::createScene());
}

void SettingView::onExit()
{
    Layer::onExit();
    
}














